﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;

namespace TrainingsApp.IPADApp.UserControls.Main {
    public partial class UC_Register : ControlBase {
        protected void Page_Load(object sender, EventArgs e) {

            if (!IsPostBack) {
                TemplateUserInfoManager.Clear();
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e) {
            string userName = this.txtUserName.Text;
            TS_User_Entity entity = new TS_User_Entity();
            entity.UserName = userName;
            entity.Password = this.txtPassword.Text;
            entity.UserType = ZJFramework10.Utility.FormManager.GetValue<int>("userType", 1);
            entity.Mobile = this.txtMobile.Text;
            entity.Email = this.txtEmail.Text;
            entity.Gender = ZJFramework10.Utility.FormManager.GetValue<int>("gender", 1) == 1 ? true : false;

            TemplateUserInfoManager.Push(entity);

            if (entity.UserType == (int)UserType.Children) {
                Response.Redirect(this.CurPage.RouteHelper.Redirct("/student/ResiterDetail.aspx"));
            }
            else {
                Response.Redirect(this.CurPage.RouteHelper.Redirct("/teacher/RegisterDetail.aspx"));
            }
        }
    }


    public class TemplateUserInfoManager {

        const string SESSION_TEMPLAT_USER_INFO = "SESSION_TEMPLAT_USER_INFO";
        public static void Push(TS_User_Entity entity) {
            HttpContext.Current.Session[SESSION_TEMPLAT_USER_INFO] = entity;
        }

        public static void Clear() {
            HttpContext.Current.Session.Remove(SESSION_TEMPLAT_USER_INFO);
        }

        public static bool TryGet(out TS_User_Entity entity) {
            entity = null;
            object o = HttpContext.Current.Session[SESSION_TEMPLAT_USER_INFO];
            if (o == null) return false;
            entity = o as TS_User_Entity;
            if (entity == null) return false;
            return true;
        }
    }
}